package com.wxw.BaseLibrary.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.wxw.BaseLibrary.R;
import com.wxw.BaseLibrary.component.SpinnerDropDownView;
import com.wxw.BaseLibrary.component.SpinnerView;

import java.util.List;

/**
 * Created by david on 2014-07-09.
 */
public class SpinnerAdapter<T> extends ArrayAdapter<T> {

    protected int[] dropDownBackColor = new int[]{ Color.parseColor("#aff5ff") };
    protected int dropDownBorderColor = Color.parseColor("#17a0ff");
    protected int dropDownBorderWidth = 1;
    protected int dropDownCornerRadius = 0;

    protected int verticalMargin = 10;
    protected int[] backColor = new int[]{ Color.parseColor("#aff5ff") };
    protected int borderColor = Color.parseColor("#17a0ff");
    protected int borderWidth = 1;
    protected int cornerRadius = 3;
    protected int textSize = 18;
    protected int textColor = Color.BLACK;
    protected int downIconID = R.drawable.dropdown;
    protected int textViewResourceId = 0;
    protected int dropDownViewResourceId = 0;

    public SpinnerAdapter(Context context, int textViewResourceId, List<T> objects, int downIconID) {
        super(context, textViewResourceId, objects);
        this.downIconID = downIconID;
        this.textViewResourceId = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        T obj = (T)getItem(position);
        if (textViewResourceId > 0) {
            convertView = LayoutInflater.from(getContext()).inflate(textViewResourceId, null);
            if (convertView instanceof TextView){
                ((TextView)convertView).setText(obj.toString());
                ((TextView)convertView).setTextSize(getTextSize());
            } else if (convertView instanceof LinearLayout) {
                LinearLayout linearLayout = (LinearLayout)convertView;
                for (int i=0;i<linearLayout.getChildCount();i++) {
                    if (linearLayout.getChildAt(i) instanceof  TextView) {
                        ((TextView)linearLayout.getChildAt(i)).setText(obj.toString());
                        ((TextView)linearLayout.getChildAt(i)).setTextSize(getTextSize());
                        break;
                    }
                }
            }
        } else {
            SpinnerView spinnerView = new SpinnerView(getContext(), downIconID);
            spinnerView.getTextView().setText(obj.toString());
            spinnerView.setTextSize(textSize);
            try {
                spinnerView.setVerticalMargin(verticalMargin);
                spinnerView.setBackColor(backColor);
                spinnerView.setBorderColor(borderColor);
                spinnerView.setBorderWidth(borderWidth);
                spinnerView.setCornerRadius(cornerRadius);
                spinnerView.setTextColor(textColor);
//                spinnerView.setDownIconID(downIconID);
                spinnerView.setBackgroundStyleResourceId(textViewResourceId);
                spinnerView.refreshView();
            } catch (Exception e) {
                Log.e("PSA-TAG", e.getMessage());
            }
            convertView = spinnerView;
        }
        convertView.setTag(obj);
        return convertView;
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        T obj = (T)getItem(position);
        if (dropDownViewResourceId > 0) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(dropDownViewResourceId, null);
            }
            if (convertView instanceof TextView){
                ((TextView)convertView).setText(obj.toString());
            }
        } else {
            if (convertView == null) {
                convertView = new SpinnerDropDownView(getContext());
            }
            ((SpinnerDropDownView) convertView).getTextView().setText(obj.toString());
            ((SpinnerDropDownView) convertView).setTextSize(textSize);
            try {
                ((SpinnerDropDownView) convertView).setBackColor(dropDownBackColor);
                ((SpinnerDropDownView) convertView).setBorderColor(dropDownBorderColor);
                ((SpinnerDropDownView) convertView).setBorderWidth(dropDownBorderWidth);
                ((SpinnerDropDownView) convertView).setCornerRadius(0);
                ((SpinnerDropDownView) convertView).setTextColor(textColor);
                ((SpinnerDropDownView) convertView).refreshView();
            } catch (Exception e) {
                Log.e("PSA-TAG", e.getMessage());
            }
        }
        return convertView;
    }

    @Override
    public void setDropDownViewResource(int resource) {
        this.dropDownViewResourceId = resource;
    }

    public int getVerticalMargin() {
        return verticalMargin;
    }

    public void setVerticalMargin(int verticalMargin) {
        this.verticalMargin = verticalMargin;
    }

    public int[] getBackColor() {
        return backColor;
    }

    public void setBackColor(int[] backColor) {
        this.backColor = backColor;
    }

    public int getBorderColor() {
        return borderColor;
    }

    public void setBorderColor(int borderColor) {
        this.borderColor = borderColor;
    }

    public int getBorderWidth() {
        return borderWidth;
    }

    public void setBorderWidth(int borderWidth) {
        this.borderWidth = borderWidth;
    }

    public int getCornerRadius() {
        return cornerRadius;
    }

    public void setCornerRadius(int cornerRadius) {
        this.cornerRadius = cornerRadius;
    }

    public int getTextSize() {
        return textSize;
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
    }

    public int getTextColor() {
        return textColor;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    public int[] getDropDownBackColor() {
        return dropDownBackColor;
    }

    public void setDropDownBackColor(int[] dropDownBackColor) {
        this.dropDownBackColor = dropDownBackColor;
    }

    public int getDropDownBorderColor() {
        return dropDownBorderColor;
    }

    public void setDropDownBorderColor(int dropDownBorderColor) {
        this.dropDownBorderColor = dropDownBorderColor;
    }

    public int getDropDownBorderWidth() {
        return dropDownBorderWidth;
    }

    public void setDropDownBorderWidth(int dropDownBorderWidth) {
        this.dropDownBorderWidth = dropDownBorderWidth;
    }

    public int getDropDownCornerRadius() {
        return dropDownCornerRadius;
    }

    public void setDropDownCornerRadius(int dropDownCornerRadius) {
        this.dropDownCornerRadius = dropDownCornerRadius;
    }

    public int getDownIconID() {
        return downIconID;
    }

    public void setDownIconID(int downIconID) {
        this.downIconID = downIconID;
    }
}
